package org.railage.server.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.railage.server.HibernateUtil;
import org.railage.server.entities.Route;

public class RouteDAOImpl implements RouteDAO {

	private static final Logger log = Logger.getLogger(RouteDAOImpl.class);

	public int addRoute(String routeName, String stations,
			String timesAfterPrev, String timesOnStations) {
		Session session = null;
		Transaction transaction = null;
		int result = -1;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			Query query = session
					.createSQLQuery("select fn_add_route(:routeName,:stations,:timesAfterPrev,:timesOnStations)");
			query.setString("routeName", routeName);
			query.setString("stations", stations);
			query.setString("timesAfterPrev", timesAfterPrev);
			query.setString("timesOnStations", timesOnStations);
			result = (Integer) query.uniqueResult();
			transaction.commit();
		} catch (Exception e) {
			log.error("Error occured during Hibernate session:", e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return result;
	}

	public int deleteRoute(String routeName) {
		Session session = null;
		Transaction transaction = null;
		int result = -1;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			Query query = session
					.createSQLQuery("select fn_delete_route(:routeName)");
			query.setString("routeName", routeName);
			result = (Integer) query.uniqueResult();
			transaction.commit();
		} catch (Exception e) {
			log.error("Error occured during Hibernate session:", e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<Route> getAllRoutes() {
		Session session = null;
		List<Route> routes = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			routes = (List<Route>) session.createCriteria(Route.class).list();
		} catch (Exception e) {
			log.error("Error occured during Hibernate session:", e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return routes;
	}

	public Route getRouteByTripID(int tripID) {
		Session session = null;
		Route route = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Query query = session
					.createQuery("from Route where id in (select route from Trip where id = :tripID)");
			query.setInteger("tripID", tripID);
			route = (Route) query.list().get(0);
		} catch (Exception e) {
			log.error("Error occured during Hibernate session:", e);
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return route;
	}

}
